package com.ydd.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydd.dto.SysUserLoginDTO;
import com.ydd.dto.SysUserPageQueryDTO;
import com.ydd.entity.SysRole;
import com.ydd.entity.SysUser;
import com.ydd.entity.user;
import com.ydd.vo.SysUserLoginVo;
import com.ydd.constant.MessageConstant;
import com.ydd.mapper.SysUserMapper;
import com.ydd.result.Result;
import com.ydd.service.SysUserService;
import com.ydd.utils.JwtUtil;
import com.ydd.vo.SysUserPageQueryVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author zhangxiaojun
 * @since 2023-05-14
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired(required = false)
    SysUserMapper sysUserMapper;

    /**
     * 登录接口
     *
     * @param dto
     * @return
     */
    @Override
    public Result login(SysUserLoginDTO dto) {
        //根据账号查询结果
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name",dto.getLogin_name());
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
        //如果账号不存在直接返回,账号不存在
        if (sysUser == null){
            return Result.error(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //对密码加密后再进行校验,使用md5
        String Password = DigestUtils.md5DigestAsHex(dto.getPassword().getBytes());


        //根据账号和密码查询
        queryWrapper.eq("password",Password);
        SysUser user = sysUserMapper.selectOne(queryWrapper);
        //账号查询到但密码不一样,返回密码错误
        if (user == null){
            return Result.error(MessageConstant.PASSWORD_ERROR);
        }

        //生成token并返回给前端
        String token = JwtUtil.getToken(user.getId());
        SysUserLoginVo vo = SysUserLoginVo.builder()
                .token(token)
                .build();
        return Result.success(vo);
    }

    //分页
    @Override
    public SysUserPageQueryVO pageQuery(SysUserPageQueryDTO sysUserPageQueryDTO) {
        //创建分页助手
        Page<SysUser> page = new Page<>(Integer.valueOf(sysUserPageQueryDTO.getPage()),
                Integer.valueOf(sysUserPageQueryDTO.getLimit()));

        //创建查询条件助手
        LambdaQueryWrapper<SysUser> lambada = new LambdaQueryWrapper();
        if (sysUserPageQueryDTO.getLoginName() != null && sysUserPageQueryDTO.getLoginName() !=""){
            lambada.like( SysUser::getLoginName, sysUserPageQueryDTO.getLoginName());
        }
        if (sysUserPageQueryDTO.getPhone() != null && sysUserPageQueryDTO.getPhone() !=""){
            lambada.like( SysUser::getPhone, sysUserPageQueryDTO.getPhone());
        }
        if (sysUserPageQueryDTO.getStatus() != null && sysUserPageQueryDTO.getStatus() !=""){
            lambada.like( SysUser::getStatus, sysUserPageQueryDTO.getStatus());
        }

        //查询
        Page<SysUser> result = sysUserMapper.selectPage(page,lambada);

        //返回数据
        return SysUserPageQueryVO.builder()
                .count(result.getTotal())
                .totalPages(result.getPages())
                .pageSize(result.getSize())
                .currentPage(result.getCurrent())
                .data(result.getRecords())
                .build();
    }

    //管理员添加
    @Override
    public void add(SysUser sysUser) {
        //补充数据:设置新增管理员的初始时间
        sysUser.setCreateDate(LocalDateTime.now());
        //调用数据库的新增方法
        sysUserMapper.add(sysUser);
    }

    //管理员详情
    @Override
    public SysUser rowInfo(String id) {
        //调用BaseMapper中的selectById方法
        SysUser sysUser =sysUserMapper.selectById(id);
        return sysUser;
    }

    //删除
    @Override
    public void del(String id) {
        sysUserMapper.deleteById(id);
    }

    //修改
    @Override
    public void update(SysUser sysUser) {
        sysUser.setUpdateDate(LocalDateTime.now());
        sysUserMapper.updateById(sysUser);
    }


}
